﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using System.Windows.Threading;
using YoucaiReader.Pages;
using Assistant;
using System.Windows.Input;
using System.Windows.Media.Imaging;

namespace YoucaiReader.UserControls
{
    public partial class BrightControl : UserControl
    {
        private DispatcherTimer UpdateTimeMinus = new DispatcherTimer();

        private DispatcherTimer UpdateTimePlus = new DispatcherTimer();
        public BrightControl()
        {
            this.InitializeComponent();
            //if (!App.ReaderPage_Night)
            //{
            //    double bright =  (double)SettingHelper.GetValueFromSettings("ReaderPage_Bright") * 100;
            //    this.BrightSlider.Value = 80 - bright;
            //}
            //else
            //{
            //    this.BrightSlider.Value = 80 - (double)SettingHelper.GetValueFromSettings("ReaderPage_NightBright") * 100;
            //}
            //this.BrightText_Update();
        }

        private bool isNew = false;
        private BookReaderPage bookReaderPage;
        public BrightControl(BookReaderPage bookReaderPage, bool isNew = true)
            : this()
        {
            this.isNew = true;
            this.bookReaderPage = bookReaderPage;
            if (!App.ReaderPage_Night)
            {
                double bright = (double)SettingHelper.GetValueFromSettings("ReaderPage_Bright") * 100;
                this.BrightSlider.Value = 80 - bright;
            }
            else
            {
                this.BrightSlider.Value = 80 - (double)SettingHelper.GetValueFromSettings("ReaderPage_NightBright") * 100;
            }
            this.BrightText_Update();
        }

        private void BrightMinus_MouseLeave(object sender, MouseEventArgs e)
        {
            this.UpdateTimeMinus.Stop();
            this.UpdateTimeMinus.Tick -= new EventHandler(this.UpdateTimeMinus_Tick);
        }

        private void BrightMinus_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            this.BrightMinus_SliderValue();
            this.UpdateTimeMinus.Tick += new EventHandler(this.UpdateTimeMinus_Tick);
            this.UpdateTimeMinus.Start();
            this.UpdateTimeMinus.Interval = TimeSpan.FromMilliseconds(100);
        }

        private void BrightMinus_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            this.UpdateTimeMinus.Stop();
            this.UpdateTimeMinus.Tick -= new EventHandler(this.UpdateTimeMinus_Tick);
        }

        private void BrightMinus_SliderValue()
        {
            if (this.BrightSlider.Value - 0.8 < 0)
            {
                this.BrightSlider.Value = 0;
                return;
            }
            Slider brightSlider = this.BrightSlider;
            brightSlider.Value = brightSlider.Value - 0.8;
        }

        private void BrightPlus_MouseLeave(object sender, MouseEventArgs e)
        {
            this.UpdateTimePlus.Stop();
            this.UpdateTimePlus.Tick -= new EventHandler(this.UpdateTimePlus_Tick);
        }

        private void BrightPlus_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            this.BrightPlus_SliderValue();
            this.UpdateTimePlus.Tick += new EventHandler(this.UpdateTimePlus_Tick);
            this.UpdateTimePlus.Start();
            this.UpdateTimePlus.Interval = TimeSpan.FromMilliseconds(100);
        }

        private void BrightPlus_MouseLeftButtonUp(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            this.UpdateTimePlus.Stop();
            this.UpdateTimePlus.Tick -= new EventHandler(this.UpdateTimePlus_Tick);
        }

        private void BrightPlus_SliderValue()
        {
            if (this.BrightSlider.Value + 0.8 > 80)
            {
                this.BrightSlider.Value = 80;
                return;
            }
            Slider brightSlider = this.BrightSlider;
            brightSlider.Value = brightSlider.Value + 0.8;
        }

        private void BrightSlider_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
        {
            double number = (80 - this.BrightSlider.Value > 0 ? (80 - this.BrightSlider.Value) / 100 : 0);
            //this.PopupBrightPanel((80 - this.BrightSlider.Value > 0 ? (80 - this.BrightSlider.Value) / 100 : 0));
            this.bookReaderPage.BookReader_BrightMenu(number);
            this.BrightText_Update();
        }

        private void BrightText_Update()
        {
            TextBlock brightText = this.BrightText;
            int value = (int)(this.BrightSlider.Value * 1.25);
            brightText.Text = value.ToString();
            this.UpdateButton_Value();
        }

        private void UpdateButton_Value()
        {
            if (this.BrightSlider.Value == 0)
            {
                this.BrightPlus.Source = new BitmapImage(new Uri("/Images/ReadIcon/lightup_nor.png", UriKind.Relative));
                this.BrightMinus.Source = new BitmapImage(new Uri("/Images/ReadIcon/lightdown_nor2.png", UriKind.Relative));
                return;
            }
            if (this.BrightSlider.Value == 80)
            {
                this.BrightPlus.Source = new BitmapImage(new Uri("/Images/ReadIcon/lightup_nor2.png", UriKind.Relative));
                this.BrightMinus.Source = new BitmapImage(new Uri("/Images/ReadIcon/lightdown_nor.png", UriKind.Relative));
                return;
            }
            this.BrightPlus.Source = new BitmapImage(new Uri("/Images/ReadIcon/lightup_nor.png", UriKind.Relative));
            this.BrightMinus.Source = new BitmapImage(new Uri("/Images/ReadIcon/lightdown_nor.png", UriKind.Relative));
        }

        private void UpdateTimeMinus_Tick(object sender, EventArgs e)
        {
            this.BrightMinus_SliderValue();
        }

        private void UpdateTimePlus_Tick(object sender, EventArgs e)
        {
            this.BrightPlus_SliderValue();
        }
    }
}
